<template>
	<el-container>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" :disabled="is_recycle" icon="el-icon-plus" @click="add"></el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0"
							   @click="batch_del"></el-button>
					<el-button type="primary"  @click="exportForm">导出表格</el-button>
					<!--客户列表和回收站切换-->
					<el-radio-group v-model="is_recycle" size="default" style="margin-left:20px"
									@change="is_recycle_Change">
						<el-radio-button :label=0>客户列表</el-radio-button>
						<el-radio-button :label=1>客户列表回收站</el-radio-button>
					</el-radio-group>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.realname" placeholder="客户名称" @clear="clearSearch"
								  clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort
						 remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label=全称 prop="name" width="150"></el-table-column>
					<el-table-column label=简称 prop="simple_name" width="100"></el-table-column>
					<el-table-column label="联系人" prop="tel_name" width="100"></el-table-column>
					<el-table-column label="联系地址" prop="address" ></el-table-column>
					<el-table-column label="职位名称" prop="role_name" width="100"
									 column-key="filterUserName"></el-table-column>
					<el-table-column label="分类" prop="class_name" width="50"
									 column-key="filterUserName"></el-table-column>
					<el-table-column label="类别" prop="type" width="100"></el-table-column>
					<el-table-column   label="状态" prop="status" width="60"  v-if="!is_recycle">
						<template #default="scope">
							<sc-status-indicator v-if="scope.row.status" type="success"></sc-status-indicator>
							<sc-status-indicator v-if="!scope.row.status" pulse type="danger"></sc-status-indicator>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="right" >
						<template #default="scope">
							<el-button-group>
								<template v-if="!is_recycle">
									<el-button text type="primary" size="small"
											   @click="table_show(scope.row, scope.$index)">查看
									</el-button>
									<el-button text type="primary" size="small"
											   @click="table_edit(scope.row, scope.$index)">编辑
									</el-button>
									<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">删除</el-button>
										</template>
									</el-popconfirm>
								</template>
								<el-button-group v-if="is_recycle">
									<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">永久删除</el-button>
										</template>
									</el-popconfirm>
									<el-popconfirm title="确定还原吗？"
												   @confirm="table_restore(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">还原</el-button>
										</template>
									</el-popconfirm>
								</el-button-group>
							</el-button-group>
						</template>
					</el-table-column>

				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @reloadData="reloadData" @success="handleSuccess"
				 @closed="dialog.save=false"></save-dialog>


</template>

<script>
import saveDialog from './save'
import $config from '@/config'

export default {
	name: 'companyCustomer',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false,
				permission: false
			},
			showGrouploading: false,
			groupFilterText: '',
			group: [],
			apiObj: this.$API.company.CompanyCustomer.list,
			selection: [],
			search: {
				realname: null
			},
			is_recycle: 0
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
		// this.getGroup()
	},
	methods: {
		// 导出表格
		async exportForm(){
			console.log('导出表格')
			const params ={
				is_export:1,
				is_recycle:this.is_recycle
			}
			let res = await this.$API.company.CompanyCustomer.export.get(params);
			console.log(res,"export res")
			// console.log(`${$config.API_URL}${res.data.file}`,'------url-----------')
			window.location.href=`${$config.API_URL}${res.data.file}`
		},
		is_recycle_Change(value) {
			this.$refs.table.upData({is_recycle: value});
		},
		// 子组件刷新数据C
		reloadData() {
			this.$refs.table.reload();
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row, index) {
			let reqData = {
				ids: row.id,
				is_recycle: this.is_recycle
			}
			let res = await this.$API.company.CompanyCustomer.delete.post(reqData);
			if (res.code === 0) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const loading = this.$loading();
				const ids = this.selection.map(ele => ele.id)
				var res = await this.$API.company.CompanyCustomer.delete.post({ids: ids, is_recycle: this.is_recycle});
				if (res.code === 0) {
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {

			})
		},
		// 还原
		async table_restore(row, index) {
			var res = await this.$API.company.CompanyCustomer.edit.post({id: row.id, is_reduction: 1});
			if (res.code == 0) {
				this.$refs.table.tableData.splice(index, 1);
				this.$message.success("还原成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//加载树数据
		async getGroup() {
			this.showGrouploading = true;
			var res = await this.$API.system.dept.list.get();
			this.showGrouploading = false;
			var allNode = {id: '', label: '所有'}
			res.data.unshift(allNode);
			this.group = res.data;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		// 清除搜索
		clearSearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				data.id = new Date().getTime()
				this.$refs.table.tableData.unshift(data)
			} else if (mode == 'edit') {
				this.$refs.table.tableData.filter(item => item.id === data.id).forEach(item => {
					Object.assign(item, data)
				})
			}
		}
	}
}
</script>

<style lang="sass" scoped>

</style>
